﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using DataRecv.enums;
using DataRecv.models.device;
using DataRecv.models.road;
using System.Collections;

namespace DataRecv
{
    public partial class editcamera : Form
    {
        public string roadid;
        public string roadname;
        public string ip;
        public string type;
        public string workmode;
        public string direction;
        public string speedlimit;
        public string bz;
        public string picwidth;
        public string picheight;
        public string sbbh;
        public string deviceusername;
        public string devicepassword;
        public string mutiroad;
        public string channel1roadid;
        public string channel2roadid;
        public string channel3roadid;
        public string channel4roadid;
        public string channel5roadid;
        public string channel6roadid;
        public string channel7roadid;
        public string channel8roadid;
        public string djsnapbyplate;
        public string platetypesnapby;
        public string djsnapbytime;
        public string time1_start;
        public string time1_end;
        public string time2_start;
        public string time2_end;
        public string time3_start;
        public string time3_end;
        public string time4_start;
        public string time4_end;
        public string staticpicenable;
        public string staticpicindex;
        public string staticpic;
        public int rowindex;

        public editcamera()
        {
            InitializeComponent();
        }

        private void editcamera_Load(object sender, EventArgs e)
        {
            try
            {
                this.comboBox_camtype.DataSource = Enum.GetNames(typeof(Enum_DeviceType));
                this.comboBox_camdirection.DataSource = Enum.GetNames(typeof(Enum_Direction));
                List<string> roadlist0 = new List<string>();
                List<string> roadlist1 = new List<string>();
                List<string> roadlist2 = new List<string>();
                List<string> roadlist3 = new List<string>();
                List<string> roadlist4 = new List<string>();
                List<string> roadlist5 = new List<string>();
                List<string> roadlist6 = new List<string>();
                List<string> roadlist7 = new List<string>();
                List<string> roadlist8 = new List<string>();
                this.comboBox_camroad.Items.Clear();
                if (G.Roads != null)
                {
                    IDictionaryEnumerator ide = G.Roads.GetEnumerator();
                    ide.Reset();
                    while (ide.MoveNext())
                    {
                        string item = ide.Key.ToString() + "---" + ((Road)ide.Value).param.roadname;
                        roadlist0.Add(item);
                        roadlist1.Add(item);
                        roadlist2.Add(item);
                        roadlist3.Add(item);
                        roadlist4.Add(item);
                        roadlist5.Add(item);
                        roadlist6.Add(item);
                        roadlist7.Add(item);
                        roadlist8.Add(item);
                    }
                }
                this.comboBox_camroad.DataSource = roadlist0;
                this.comboBox_channel1road.DataSource = roadlist1;
                this.comboBox_channel2road.DataSource = roadlist2;
                this.comboBox_channel3road.DataSource = roadlist3;
                this.comboBox_channel4road.DataSource = roadlist4;
                this.comboBox_channel5road.DataSource = roadlist5;
                this.comboBox_channel6road.DataSource = roadlist6;
                this.comboBox_channel7road.DataSource = roadlist7;
                this.comboBox_channel8road.DataSource = roadlist8;


                this.comboBox_camroad.Text = roadid + "---" + roadname;
                this.comboBox_camtype.Text = type;
                this.textBox_camip.Text = ip;
                this.comboBox_camdirection.Text = direction;
                this.textBox_sbbh.Text = sbbh;
                this.textBox_xs.Text = speedlimit;
                this.textBox_user.Text = deviceusername;
                this.textBox_password.Text = devicepassword;
                foreach (string s in roadlist0)
                {
                    if (s.StartsWith(channel1roadid+"---"))  this.comboBox_channel1road.Text = s;
                    if (s.StartsWith(channel2roadid + "---")) this.comboBox_channel2road.Text = s;
                    if (s.StartsWith(channel3roadid + "---")) this.comboBox_channel3road.Text = s;
                    if (s.StartsWith(channel4roadid + "---")) this.comboBox_channel4road.Text = s;
                    if (s.StartsWith(channel5roadid + "---")) this.comboBox_channel5road.Text = s;
                    if (s.StartsWith(channel6roadid + "---")) this.comboBox_channel6road.Text = s;
                    if (s.StartsWith(channel7roadid + "---")) this.comboBox_channel7road.Text = s;
                    if (s.StartsWith(channel8roadid + "---")) this.comboBox_channel8road.Text = s;
                }
                if(djsnapbyplate!="") 
                {
                    this.checkBox_djsnapbyplateEnable.Checked = bool.Parse(djsnapbyplate);
                }
                if (platetypesnapby != "")
                {
                    this.checkBox_byblue.Checked = platetypesnapby.Contains(Enum_PlateTypeSnapBy.抓拍蓝牌.ToString());
                    this.checkBox_byyellow.Checked = platetypesnapby.Contains(Enum_PlateTypeSnapBy.抓拍黄牌.ToString());
                    this.checkBox_bynoplate.Checked=platetypesnapby.Contains(Enum_PlateTypeSnapBy.抓拍无牌.ToString());
                }
                if (djsnapbytime != "")
                {
                    this.checkBox_djsnapbytimeEnable.Checked = bool.Parse(djsnapbytime);
                }
                
                dateTimePicker_time1_start.Text = time1_start;
                dateTimePicker_time1_end.Text = time1_end;

                dateTimePicker_time2_start.Text = time2_start;
                dateTimePicker_time2_end.Text = time2_end;

                dateTimePicker_time3_start.Text = time3_start;
                dateTimePicker_time3_end.Text = time3_end;

                dateTimePicker_time3_start.Text = time3_start;
                dateTimePicker_time3_end.Text = time3_end;

                dateTimePicker_time4_start.Text = time4_start;
                dateTimePicker_time4_end.Text = time4_end;

                if(staticpicenable!="")checkBox_staticpicEnable.Checked = bool.Parse(staticpicenable);
                if (staticpicindex != "") numericUpDown_staticpicindex.Value = int.Parse(staticpicindex);
                textBox_staticpic.Text = staticpic;
                this.checkBox_mutiroad.Checked = bool.Parse(mutiroad);
                this.textBox_cambz.Text = bz;

                this.textBox_picwidth.Text = picwidth;
                this.textBox_picheight.Text = picheight;

            }
            catch { }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox_camroad.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未选择所属路口";
                    return;
                }
                if (comboBox_camtype.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未选择设备型号";
                    return;
                }
                if (textBox_camip.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未填写设备IP";
                    return;
                }
                if (textBox_sbbh.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未填写设备编号";
                    return;
                }
                if (comboBox_camdirection.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未选择监控方向";
                    return;
                }
                if (checkedListBox_workmode.CheckedItems.Count <= 0)
                {
                    toolStripStatusLabel1.Text = "修改失败，未选择工作模式";
                    return;
                }

                Enum_CaptureSystem temp_workmode = Enum_CaptureSystem.NONE;
                if (checkedListBox_workmode.CheckedItems.Count > 0)
                {
                    foreach (object obj in checkedListBox_workmode.CheckedItems)
                    {
                        temp_workmode |= ((Enum_CaptureSystem)obj);
                    }
                }

                Enum_PlateTypeSnapBy temp_snapby = Enum_PlateTypeSnapBy.抓拍全部车牌;
                if (checkBox_byblue.Checked) temp_snapby |= Enum_PlateTypeSnapBy.抓拍蓝牌;
                if (checkBox_byyellow.Checked) temp_snapby |= Enum_PlateTypeSnapBy.抓拍黄牌;
                if (checkBox_bynoplate.Checked) temp_snapby |= Enum_PlateTypeSnapBy.抓拍无牌;


                if ((temp_workmode == (temp_workmode|Enum_CaptureSystem.超速)) && (this.textBox_xs.Text == ""))
                {
                    toolStripStatusLabel1.Text = "修改失败，未设置限速值";
                    return;
                }
                if (textBox_user.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未填写用户名";
                    return;
                }
                if (textBox_password.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未填写密码";
                    return;
                }
                if (textBox_picwidth.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未填写水平分辨率";
                    return;
                }
                if (textBox_picheight.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，未填写垂直分辨率";
                    return;
                }
                if (checkBox_djsnapbytimeEnable.Checked)
                {
                    int hh_start = 0;
                    int mm_start = 0;
                    int hh_end = 0;
                    int mm_end = 0;
                    hh_start = dateTimePicker_time1_start.Value.Hour;
                    mm_start = dateTimePicker_time1_start.Value.Minute;
                    hh_end = dateTimePicker_time1_end.Value.Hour;
                    mm_end = dateTimePicker_time1_end.Value.Minute;
                    if (((hh_start == hh_end) && (mm_start > mm_end)) || (hh_start > hh_end))
                    {
                        toolStripStatusLabel1.Text = "修改失败，时段1无效";
                        return;
                    }

                    hh_start = dateTimePicker_time2_start.Value.Hour;
                    mm_start = dateTimePicker_time2_start.Value.Minute;
                    hh_end = dateTimePicker_time2_end.Value.Hour;
                    mm_end = dateTimePicker_time2_end.Value.Minute;
                    if (((hh_start == hh_end) && (mm_start > mm_end)) || (hh_start > hh_end))
                    {
                        toolStripStatusLabel1.Text = "修改失败，时段2无效";
                        return;
                    }

                    hh_start = dateTimePicker_time3_start.Value.Hour;
                    mm_start = dateTimePicker_time3_start.Value.Minute;
                    hh_end = dateTimePicker_time3_end.Value.Hour;
                    mm_end = dateTimePicker_time3_end.Value.Minute;
                    if (((hh_start == hh_end) && (mm_start > mm_end)) || (hh_start > hh_end))
                    {
                        toolStripStatusLabel1.Text = "修改失败，时段3无效";
                        return;
                    }

                    hh_start = dateTimePicker_time4_start.Value.Hour;
                    mm_start = dateTimePicker_time4_start.Value.Minute;
                    hh_end = dateTimePicker_time4_end.Value.Hour;
                    mm_end = dateTimePicker_time4_end.Value.Minute;
                    if (((hh_start == hh_end) && (mm_start > mm_end)) || (hh_start > hh_end))
                    {
                        toolStripStatusLabel1.Text = "修改失败，时段4无效";
                        return;
                    }
                }

                if (checkBox_staticpicEnable.Checked == true)
                {
                    if (textBox_staticpic.Text == "")
                    {
                        toolStripStatusLabel1.Text = "修改失败，未选择静态标牌图";
                        return;
                    }
                }

                DataGridView dgv = ((camera)(this.Owner)).dataGridView1;
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    DataGridViewRow r = dgv.Rows[i];
                    if (textBox_camip.Text == r.Cells["ip"].Value.ToString() && (i != rowindex))
                    {
                        toolStripStatusLabel1.Text = "修改失败，设备IP已存在";
                        return;
                    }
                    //不校验设备编号，测速设备各个方向设备公用一个设备编号
                    //if (textBox_sbbh.Text == r.Cells["sbbh"].Value.ToString() && (i != rowindex))
                    //{
                    //    toolStripStatusLabel1.Text = "修改失败，设备编号已存在";
                    //    return;
                    //}
                }

                string thisroadid = Getroadid(comboBox_camroad.Text);
                string thisroadname = Getroadname(comboBox_camroad.Text);

                DeviceParam d = new DeviceParam();
                d.ip = textBox_camip.Text;
                d.roadid = thisroadid;
                d.sbbh = textBox_sbbh.Text;
                d.type = (Enum_DeviceType)Enum.Parse(typeof(Enum_DeviceType), comboBox_camtype.Text);
                d.workmode = temp_workmode;
                d.direction = (Enum_Direction)Enum.Parse(typeof(Enum_Direction), comboBox_camdirection.Text);
                d.speedlimit = textBox_xs.Text;
                d.deviceusername = textBox_user.Text;
                d.devicepassword = textBox_password.Text;
                d.mutiroad = checkBox_mutiroad.Checked;
                d.channel1roadid = Getroadid(comboBox_channel1road.Text);
                d.channel2roadid = Getroadid(comboBox_channel2road.Text);
                d.channel3roadid = Getroadid(comboBox_channel3road.Text);
                d.channel4roadid = Getroadid(comboBox_channel4road.Text);
                d.channel5roadid = Getroadid(comboBox_channel5road.Text);
                d.channel6roadid = Getroadid(comboBox_channel6road.Text);
                d.channel7roadid = Getroadid(comboBox_channel7road.Text);
                d.channel8roadid = Getroadid(comboBox_channel8road.Text);
                d.djsnapbyplate = checkBox_djsnapbyplateEnable.Checked;
                d.platetypesnapby = temp_snapby;
                d.djsnapbytime = checkBox_djsnapbytimeEnable.Checked;
                d.time1_start = dateTimePicker_time1_start.Text;
                d.time1_end = dateTimePicker_time1_end.Text;
                d.time2_start = dateTimePicker_time2_start.Text;
                d.time2_end = dateTimePicker_time2_end.Text;
                d.time3_start = dateTimePicker_time3_start.Text;
                d.time3_end = dateTimePicker_time3_end.Text;
                d.time4_start = dateTimePicker_time4_start.Text;
                d.time4_end = dateTimePicker_time4_end.Text;
                d.staticpicenable = checkBox_staticpicEnable.Checked;
                d.staticpicindex = (int)numericUpDown_staticpicindex.Value;
                d.staticpic = textBox_staticpic.Text;
                d.bz = textBox_cambz.Text;
                d.picwidth = int.Parse(textBox_picwidth.Text);
                d.picheight = int.Parse(textBox_picheight.Text);

                if (DeviceParam.EditModel(d) == true)
                {
                    DeviceView view = new DeviceView();
                    view.deviceparam = d;
                    view.roadparam = ((Road)G.Roads[d.roadid]).param;
                    G.Views[d.ip] = view;

                    dgv.Rows[rowindex].Cells["ip"].Value = d.ip;
                    dgv.Rows[rowindex].Cells["roadid"].Value = d.roadid;
                    dgv.Rows[rowindex].Cells["sbbh"].Value = d.sbbh;
                    dgv.Rows[rowindex].Cells["type"].Value = d.type.ToString();
                    dgv.Rows[rowindex].Cells["workmode"].Value = d.workmode.ToString();
                    dgv.Rows[rowindex].Cells["direction"].Value = d.direction.ToString();
                    dgv.Rows[rowindex].Cells["speedlimit"].Value = d.speedlimit;
                    dgv.Rows[rowindex].Cells["deviceusername"].Value = d.deviceusername;
                    dgv.Rows[rowindex].Cells["devicepassword"].Value = d.devicepassword;
                    dgv.Rows[rowindex].Cells["mutiroad"].Value = d.mutiroad.ToString();
                    dgv.Rows[rowindex].Cells["channel1roadid"].Value = d.channel1roadid;
                    dgv.Rows[rowindex].Cells["channel2roadid"].Value = d.channel2roadid;
                    dgv.Rows[rowindex].Cells["channel3roadid"].Value = d.channel3roadid;
                    dgv.Rows[rowindex].Cells["channel4roadid"].Value = d.channel4roadid;
                    dgv.Rows[rowindex].Cells["channel5roadid"].Value = d.channel5roadid;
                    dgv.Rows[rowindex].Cells["channel5roadid"].Value = d.channel6roadid;
                    dgv.Rows[rowindex].Cells["channel7roadid"].Value = d.channel7roadid;
                    dgv.Rows[rowindex].Cells["channel8roadid"].Value = d.channel8roadid;
                    dgv.Rows[rowindex].Cells["djsnapbyplate"].Value = d.djsnapbyplate.ToString();
                    dgv.Rows[rowindex].Cells["platetypesnapby"].Value = d.platetypesnapby.ToString();
                    dgv.Rows[rowindex].Cells["djsnapbytime"].Value = d.djsnapbytime.ToString();
                    dgv.Rows[rowindex].Cells["time1_start"].Value = d.time1_start;
                    dgv.Rows[rowindex].Cells["time1_end"].Value = d.time1_end;
                    dgv.Rows[rowindex].Cells["time2_start"].Value = d.time2_start;
                    dgv.Rows[rowindex].Cells["time2_end"].Value = d.time2_end;
                    dgv.Rows[rowindex].Cells["time3_start"].Value = d.time3_start;
                    dgv.Rows[rowindex].Cells["time3_end"].Value = d.time3_end;
                    dgv.Rows[rowindex].Cells["time4_start"].Value = d.time4_start;
                    dgv.Rows[rowindex].Cells["time4_end"].Value = d.time4_end;
                    dgv.Rows[rowindex].Cells["staticpicenable"].Value = d.staticpicenable.ToString();
                    dgv.Rows[rowindex].Cells["staticpicindex"].Value = d.staticpicindex.ToString();
                    dgv.Rows[rowindex].Cells["staticpic"].Value = d.staticpic;
                    dgv.Rows[rowindex].Cells["bz"].Value = d.bz;
                    dgv.Rows[rowindex].Cells["picwidth"].Value = d.picwidth.ToString();
                    dgv.Rows[rowindex].Cells["picheight"].Value = d.picheight.ToString();

                    ((Form_Main)this.Owner.Owner).UpDateTree();
                    toolStripStatusLabel1.Text = "修改成功";
                }
                else
                {
                    toolStripStatusLabel1.Text = "修改失败";
                }
            }
            catch (Exception er)
            {
                toolStripStatusLabel1.Text = "修改失败" + er.Message;
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string Getroadid(string roadid_roadname)
        {
            string ans = "";
            try
            {
                if (roadid_roadname != "")
                {
                    string[] ss = roadid_roadname.Split(new string[] { "---" }, StringSplitOptions.RemoveEmptyEntries);
                    if (ss != null)
                    {
                        ans = ss[0];
                    }
                }
            }
            catch { ans = ""; }
            return ans;
        }

        private string Getroadname(string roadid_roadname)
        {
            string ans = "";
            try
            {
                if (roadid_roadname != "")
                {
                    string[] ss = roadid_roadname.Split(new string[] { "---" }, StringSplitOptions.RemoveEmptyEntries);
                    if (ss != null)
                    {
                        ans = ss[1];
                    }
                }
            }
            catch { ans = ""; }
            return ans;
        }

        private void comboBox_camtype_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Enum_DeviceType type = (Enum_DeviceType)Enum.Parse(typeof(Enum_DeviceType), comboBox_camtype.Text);
                Enum_CaptureSystem ability = Enum_CaptureSystem.NONE;
                
                switch (type)
                {
                    case Enum_DeviceType.DH:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = DH.Ability;
                        break;
                    case Enum_DeviceType.HK5012:
                        
                        this.checkBox_mutiroad.Visible = true;
                        ability = HK5012.Ability;
                        break;
                    case Enum_DeviceType.HK9121:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = HK9121.Ability;
                        break;
                    case Enum_DeviceType.HK9151:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = HK9151.Ability;
                        break;
                    case Enum_DeviceType.HK986:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = HK986.Ability;
                        break;
                    case Enum_DeviceType.HK9361:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = HK9361.Ability;
                        break;
					case Enum_DeviceType.HK9371:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = HK9371.Ability;
                        break;
                    case Enum_DeviceType.HV:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = HV.Ability;
                        break;
                    case Enum_DeviceType.MaxFord:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = MaxFord.Ability;
                        break;
                    default:
                        this.checkBox_mutiroad.Checked = false;
                        this.checkBox_mutiroad.Visible = false;
                        ability = Enum_CaptureSystem.NONE;
                        break;
                }

                this.checkedListBox_workmode.Items.Clear();
                //string[] ss = Enum.GetNames(typeof(Enum_CaptureSystem));
                //foreach (string s in ss)
                //{
                //    if (ability.ToString().Contains(s))
                //    {
                //        this.checkedListBox_workmode.Items.Add(Enum.Parse(typeof(Enum_CaptureSystem), s), start ? false : workmode.ToString().Contains(s));
                //    }
                //}

                Enum_CaptureSystem[] ss = (Enum_CaptureSystem[])Enum.GetValues(typeof(Enum_CaptureSystem));
                foreach (Enum_CaptureSystem s in ss)
                {
                    if (s != Enum_CaptureSystem.NONE)
                    {
                        if (ability.Equals(ability | s))
                        {
                            if (workmode.Contains(s.ToString()))
                            {
                                this.checkedListBox_workmode.Items.Add(s, true);
                            }
                            else
                            {
                                this.checkedListBox_workmode.Items.Add(s, false);
                            }
                            
                        }
                    }
                }
            }
            catch { }
        }

        private void checkBox_mutiroad_CheckedChanged(object sender, EventArgs e)
        {
            label_c1.Visible = this.checkBox_mutiroad.Checked;
            label_c2.Visible = this.checkBox_mutiroad.Checked;
            label_c3.Visible = this.checkBox_mutiroad.Checked;
            label_c4.Visible = this.checkBox_mutiroad.Checked;
            label_c5.Visible = this.checkBox_mutiroad.Checked;
            label_c6.Visible = this.checkBox_mutiroad.Checked;
            label_c7.Visible = this.checkBox_mutiroad.Checked;
            label_c8.Visible = this.checkBox_mutiroad.Checked;
            comboBox_channel1road.Visible = this.checkBox_mutiroad.Checked;
            comboBox_channel2road.Visible = this.checkBox_mutiroad.Checked;
            comboBox_channel3road.Visible = this.checkBox_mutiroad.Checked;
            comboBox_channel4road.Visible = this.checkBox_mutiroad.Checked;
            comboBox_channel5road.Visible = this.checkBox_mutiroad.Checked;
            comboBox_channel6road.Visible = this.checkBox_mutiroad.Checked;
            comboBox_channel7road.Visible = this.checkBox_mutiroad.Checked;
            comboBox_channel8road.Visible = this.checkBox_mutiroad.Checked;
            if (checkBox_mutiroad.Checked == false)
            {
                comboBox_channel1road.Text = "";
                comboBox_channel2road.Text = "";
                comboBox_channel3road.Text = "";
                comboBox_channel4road.Text = "";
                comboBox_channel5road.Text = "";
                comboBox_channel6road.Text = "";
                comboBox_channel7road.Text = "";
                comboBox_channel8road.Text = "";
            }
        }

        private void button_search_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.FileName != "")
                {
                    textBox_staticpic.Text = openFileDialog1.FileName;
                }
            }
        }
    }
}
